using System;

using Microsoft.Xna.Framework;

using Elephant.Framework;

namespace Elephant.Utility.Components
{
    public sealed class Transform : EntityComponent
    {
        public Vector3 Position = Vector3.Zero;
        public Vector3 Scale = Vector3.One;
        public Quaternion Orientation = Quaternion.Identity;

        public Matrix World
        {
            get
            {
                // todo: relative orientation and scale (ie. if scale is 0,5 and parent scale is 1,5 localscale should be 1)
                Quaternion localOrientation = Orientation;
                Vector3 localPosition = Position;
                Vector3 localScale = Scale;

                if (Owner.Parent != null) {
                    if (Owner.Parent.Contains<Transform>()) {
                        Transform parentTransform = Owner.Parent.Find<Transform>();

                        localPosition += parentTransform.Position;
                        //localScale += parentTransform.Scale;
                        //localOrientation += parentTransform.Orientation;
                    }
                }

                return
                    Matrix.CreateFromQuaternion(localOrientation) *
                    Matrix.CreateScale(localScale) *
                    Matrix.CreateTranslation(localPosition);
            }
        }
    }
}
